<template>
  <div class="dashboard-editor-container">
    <span class="headimg"><img src="../../assets/headimg.png" alt=""></span>
    <div class="info-container">
      <span class="display_name">欢迎！{{name}}来到PS管理后台</span>
      <span style="font-size:20px;padding-top:20px;display:inline-block;">
        您可以<el-button style="margin: 0 5px;" size="mini" type="primary" @click="edit">点击</el-button>修改您的密码
      </span>
      <div>
        <img class="emptyGif" src="../../assets/Dance.gif">
      </div>
    </div>

    <!--编辑表单弹窗-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="formsrules" ref="dataForm" :model="temp" label-position="right" label-width="120px" style='width: 470px; margin-left:50px;'>
        <el-form-item label="账号：" prop="username">
          <el-input type="text" v-model="temp.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="旧密码：" prop="oldPass">
          <el-input type="password" v-model="temp.oldPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPass">
          <el-input type="password" v-model="temp.newPass" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑表单弹窗END-->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'dashboard',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value === this.temp.oldPass) {
        callback(new Error('新密码不能与旧密码相同'))
      } else if (value.length < 8) {
        callback(new Error('密码不能小于8位，请使用字母、数字排列组合'))
      } else {
        callback()
      }
    }
    return {
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogFormVisible: false,
      temp: {
        id: undefined,
        username: '',
        oldPass: '',
        newPass: ''
      },
      formsrules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        oldPass: [{ required: true, message: '请输入', trigger: 'blur' }],
        newPass: [{ validator: validatePass, trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name', 'username'
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.temp.username = this.username
    },
    edit() {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    sureEdit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const temData = Object.assign({}, this.temp)
          this.$store.dispatch('updatapass', temData).then(() => {
            this.$store.dispatch('FedLogOut').then(() => {
              location.reload()
            })
          }).catch(() => {
            this.$notify.error({
              title: '修改失败',
              message: '用户名或密码错误',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.emptyGif {
  display: block;
  width: 45%;
  margin: 0 auto;
}
.headimg{
  display: inline-block;
  float: left;
  margin-top: 25px;
}
.headimg img{
  width: 120px;
}

.dashboard-editor-container {
  background-color: #e3e3e3;
  min-height: 100vh;
  margin-top: -50px;
  padding: 100px 60px 0px;
  .pan-info-roles {
    font-size: 12px;
    font-weight: 700;
    color: #333;
    display: block;
  }
  .info-container {
    position: relative;
    margin-left: 190px;
    height: 150px;
    line-height: 200px;
    .display_name {
      display: inline-block;
      font-size: 48px;
      line-height: 48px;
      color: #212121;
      position: absolute;
      top: 25px;
    }
    @media screen and (max-width: 1280px) {
      .display_name {
        font-size: 24px;
      }
      .dashboard-editor-container .info-container {
        min-width: 368px;
      }
    }
  }
}
</style>
